﻿using Fzid_models;
using FzidTPTServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ScokTcpLib.ScokTcpServer
{
    


   public   class FzidPraser
    {
        private static int _SEQ = 0;
        public SendObj BuildOrder(int ordertype, string addr, object[] paramlist)
        {
            SendObj sobj = null;
            byte[] bts = null;
            switch (ordertype)
            {
                case FzidOrder.AFN_CONFRIM:
                    bts = buildConfrim(addr, paramlist);
                    sobj = new SendObj(ordertype, _SEQ, bts);

                    break;
                case FzidOrder.AFN_RESET:

                    break;
                case FzidOrder.AFN_SET_PARAM:
                    bts = buildSetParam(addr, paramlist);
                    sobj = new SendObj(ordertype, _SEQ, bts);
                    break;
                case FzidOrder.AFN_CONNTION_CHECK:

                    break;

            }
            return sobj;
        }
        public object ParserOrder(byte[] bts, int recvLen)
        {
            //if (!verifyFrame(bts)) {
            //    return null;
            //}
            int fn = bts[10];
            RecvObj robj = null;
            switch (fn)
            {
                case FzidOrder.AFN_ACTIVE_DATA:
                    robj = new RecvObj();
                    robj.OrderType = fn;
                    parserActiveData(ref robj, bts, recvLen);
                    if (robj.ObjValue != null)
                    {
                        robj.ObjType = "RealTimeModel";
                    }
                    else
                    {
                        robj.ObjType = "null";
                    }
                    break;

            }
            return robj;
        }

        #region
        private object parserActiveData(ref RecvObj robj, byte[] bts, int recvLen)
        {

            RealTimeModel rtm = new RealTimeModel();
            //第十六字节开始为行政区域
            rtm.AreaCode = string.Format("{0:X2}{1:X2}", bts[5], bts[6]);
            rtm.DevCode = string.Format("{0:D4}", (bts[7] << 8) | bts[8]);

            rtm.SimCode = CommonTools.getSimCode(bts, 16);
            rtm.MaxValueTime = string.Format("{0}:{1}", bts[22], bts[23]);
            rtm.MaxValue = string.Format("{0}.{1}", bts[24], bts[25]);

            rtm.MinValueTime = string.Format("{0}:{1}", bts[26], bts[27]);
            rtm.MinValue = string.Format("{0}.{1}", bts[28], bts[29]);

            rtm.AvgValue = string.Format("{0}.{1}", bts[30], bts[31]);

            rtm.Voltage = string.Format("{0}", bts[32]);
            rtm.Status = string.Format("{0}", bts[33]);


            robj.Address = string.Format("{0:X2}{1:X2}{2:X2}{3:X2}{4:X2}", bts[5], bts[6], bts[7], bts[8], bts[9]);
            rtm.Address = robj.Address;
            robj.SimCode = rtm.SimCode;

            robj.ObjValue = rtm;
            return robj;


        }
        private bool verifyFrame(byte[] bts)
        {
            if (bts[0] == bts[3] && bts[0] == 0x68 && bts[1] == bts[2])
            {
                int framelen = (bts[1] >> 2) + 8;
                if (bts.Length >= framelen && bts[framelen - 1] == 0x16)
                {
                    byte chk = 0;
                    for (int i = 4; i < framelen - 2; i++)
                    {
                        chk += bts[i];
                    }

                    if (chk == bts[framelen - 2])
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }

            }
            else
            {
                return false;
            }
        }
        private void fillOrderheader(ref byte[] bts, int ordertype, int len, string addr)
        {
            bts[0] = 0x68;
            ushort lenx = (ushort)((len << 2) | 1);

            bts[1] = (byte)(lenx);
            bts[2] = (byte)(lenx);
            bts[3] = 0x68;

            bts[4] = 10;
            if (addr.Length != 10)
            {
                bts[5] = 0x00;
                bts[6] = 0x00;
                bts[7] = 0x00;
                bts[8] = 0x00;
                bts[9] = 0x00;
                bts[10] = 0x00;
            }
            else
            {
                string qy = addr.Substring(0, 4);
                string bh = addr.Substring(4, 4);
                string fwq = addr.Substring(8, 2);

                byte[] bqy = CommonTools.hexStringToBCD(qy);
                byte[] bbh = CommonTools.hexStringToBCD(bh);
                byte[] bfwq = CommonTools.hexStringToBCD(fwq);

                Array.Copy(bqy, 0, bts, 5, 2);
                Array.Copy(bbh, 0, bts, 7, 2);
                Array.Copy(bfwq, 0, bts, 9, 1);
            }

        }
        private byte[] buildConfrim(string addr, object[] paramlist)
        {
            int datalen = 10;
            byte[] bts = new byte[datalen + 6];
            string msg = paramlist[0].ToString();
            fillOrderheader(ref bts, FzidOrder.AFN_CONFRIM, datalen, addr);
            bts[11] = 0xf0;
            if (msg.Equals("ok"))
            {
                bts[12] = 0x6f;
                bts[13] = 0x6b;

            }
            else
            {
                bts[12] = 0;
                bts[13] = 0;
            }
            bts[14] = CommonTools.CalcCheck(bts, datalen + 6);
            bts[15] = 0x16;
            return bts;
        }
        private byte[] buildSetParam(string addr, object[] paramlist)
        {
            int datalen = 36;
            byte[] bts = new byte[datalen + 6];
            fillOrderheader(ref bts, FzidOrder.AFN_SET_PARAM, datalen, addr);
            //sim卡号

            byte[] btssim = CommonTools.hexStringToBCD("0" + paramlist[0].ToString());
            Array.Copy(btssim, 0, bts, 16, 6);
            //ip地址
            string[] iparr = paramlist[1].ToString().Split('.');
            bts[22] = Convert.ToByte(iparr[0]);
            bts[23] = Convert.ToByte(iparr[1]);
            bts[24] = Convert.ToByte(iparr[2]);
            bts[25] = Convert.ToByte(iparr[3]);
            //端口号
            ushort port = Convert.ToUInt16(paramlist[2].ToString());
            bts[26] = (byte)(port >> 8);
            bts[27] = (byte)port;
            //温度下限
            ushort uplimit = Convert.ToUInt16(paramlist[3].ToString());
            bts[28] = (byte)(uplimit >> 8);
            bts[29] = (byte)uplimit;
            //温度上限
            ushort downlimit = Convert.ToUInt16(paramlist[4].ToString());
            bts[30] = (byte)(downlimit >> 8);
            bts[31] = (byte)downlimit;
            //温度测量间隔
            bts[32] = Convert.ToByte(paramlist[5].ToString());
            //上报次数
            bts[33] = Convert.ToByte(paramlist[6].ToString());
            //上报时间点1 2 3
            int sbsjd = Convert.ToByte(paramlist[7].ToString());

            bts[34] = (byte)(downlimit >> 16);

            bts[35] = (byte)(downlimit >> 8);

            bts[36] = (byte)(downlimit);

            //上报间隔
            bts[37] = Convert.ToByte(paramlist[8].ToString());
            //上报模式
            bts[38] = Convert.ToByte(paramlist[9].ToString());
            //GPRS工作模式
            bts[39] = Convert.ToByte(paramlist[10].ToString());
            //




            bts[datalen + 4] = CommonTools.CalcCheck(bts, datalen + 6);
            bts[datalen + 5] = 0x16;
            return bts;
        }
        #endregion
    }

}
